/*
 *   The contents of this file are subject to the Mozilla Public License
 *   Version 1.1 (the "License"); you may not use this file except in
 *   compliance with the License. You may obtain a copy of the License at
 *   http://www.mozilla.org/MPL/
 *
 *   Software distributed under the License is distributed on an "AS IS"
 *   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 *   License for the specific language governing rights and limitations
 *   under the License.
 *
 *   The Original Code is OkkamNGWebServices, in the package org.okkam.webservice.*.
 *
 *   The Initial Developer of the Original Code is 
 *		Heiko Stoermer (http://www.know-who.net).
 *
 *   Portions created by the Initial Developer are Copyright (C) 2007
 *   the Initial Developer. All Rights Reserved.
 *
 *   Contributor(s): Daniel Giacomuzzi (etadaniel@gmail.com).
 *
*/
package org.okkam.webservice;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.okkam.core.api.data.entityprofile.EntityProfile;

/**
 * Business delegate from web service to okkam core. Returns an {@link org.okkam.core.api.data.entityprofile.EntityProfile} for a given OKKAM URI.
 * 
 * @author stoermer
 * 
 * 16-nov-2007 etadaniel add comments to the class 
 * 16-nov-2007 etadaniel move
 * the return line from outside the try block into it and split the return into
 * 2 points (string and null)
 */
public class ProfileRetriever {
	private final static Log log = LogFactory.getLog(SearchNG.class);

	/**
	 * class builder
	 */
	public ProfileRetriever() {
	}

	/**
	 * This method build the {@link org.okkam.core.api.data.entityprofile.EntityProfile} of the passed URI and returns it as a
	 * JAXB XML String
	 * 
	 * @param uri
	 *            the uri of the EntityProfile to retrieve
	 * @return a JAXB XML string that represent the {@link org.okkam.core.api.data.entityprofile.EntityProfile}
	 */
	public String getProfileForURI(String uri) {

		StringWriter profilexml = new StringWriter();
		log.debug("called. delegating to get profile");
		org.okkam.core.api.ProfileRetriever pr = new org.okkam.core.api.ProfileRetriever();
		// get the EntityProfile
		EntityProfile profile = pr.getEntityProfileFromURI(uri);
		// marshal the profile
		log.debug("trying to marshal profile.");
		try {
			JAXBContext jc = JAXBContext.newInstance(
					"org.okkam.core.api.data.entityprofile", getClass()
							.getClassLoader());
			Marshaller m = jc.createMarshaller();
			m.marshal(profile, profilexml);
			log.debug("delegation finished. returning.");
			// this line is moved from outside the try block
			return profilexml.toString();
		} catch (JAXBException e) {
			log.error("marshalling failed: " + e.getStackTrace().toString());
			return null;
		}
	}
}
